/**
 * Created by AM on 2017/6/27.
 */
(function (window,$) {
    var add = {
        run: function () {
            self = this;
            $('#cid').blur(function () {
                $('.suv').empty();
                self.addgoods();
            })
        },
        addgoods: function () {
            var uid = $('#cid').val();
            var authUrl = $('#action').attr('action');
            $.ajax({
                url: authUrl+'/auth',
                type: 'post',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': $('#token').val()
                },
                data: window.JSON.stringify({
                    id: uid
                }),
                success: function(response) {
                    var div = $('.suv');
                    for(var i=0; i<response.title.length; i++){
                        var p = $('<p>').html(response.title[i]['title']).appendTo(div);
                            $('<input>').addClass('sku'+i).attr('value',response.title[i]['id']).appendTo(p);
                        var select = $('<select>').addClass('value'+i).appendTo(div);
                        for(var x=0; x<response.value.length; x++){
                            if(response.title[i]['id'] == response.value[x]['attr_id']){
                                $('<option>').html(response.value[x]['value']).attr('value',response.value[x]['id']).appendTo(select);
                            }
                        }

                        $('.value'+i).change(function () {
                            $('#sku').val('');
                            for (var y=0; y<response.title.length; y++)
                            document.getElementById('sku').value += $('.sku'+y).val()+'-'+$('.value'+y).val()+'_';
                        });
                    }
                },
                error: function (err) {
                    console.log(err);
                }
            })
        }
    }
    add.run();
})(window,jQuery);